package com.sailfish.springbootdemo.dao.db7;

import com.sailfish.springbootdemo.common.DB7TableName;
import com.sailfish.springbootdemo.pojo.db7.TbDataDictionary;
import com.sailfish.springbootdemo.pojo.db7.TbTemplateVersion;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface TbTemplateVersionDao {

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TEMPLATE_VERSION +
            " where 1=1 " +
            "<if test='template_type != null'>" +
            " and template_type = #{template_type}" +
            "</if>" +
            "<if test='flow_no != null'>" +
            " and flow_no = #{flow_no}" +
            "</if>" +
            "<if test='version != null'>" +
            " and version = #{version}" +
            "</if>" +
            "<if test='status != null'>" +
            " and status = #{status}" +
            "</if>" +
            " order by version desc " +
            "</script>")
    List<TbTemplateVersion> getTemplateVersionList(TbTemplateVersion tbTemplateVersion);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TEMPLATE_VERSION +
            " where 1=1" +
            "<if test='template_type != null and template_type!=\"\"'>" +
            " and template_type = #{template_type}" +
            "</if>" +
            " order by sort desc limit 1" +
            "</script>")
    List<TbTemplateVersion> getNewestTemplateVersion(@Param("template_type") String template_type);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TEMPLATE_VERSION +
            " where 1=1 and status=3 " +
            "<if test='template_type != null and template_type!=\"\"'>" +
            " and template_type = #{template_type}" +
            "</if>" +
            " order by sort desc limit 1" +
            "</script>")
    List<TbTemplateVersion> getNewestTemplateVersion2(@Param("template_type") String template_type);



    @Insert("insert into " + DB7TableName.TB_TEMPLATE_VERSION + " " +
            "(template_type, version, create_time, create_user, update_time, update_user, status, sort,flow_no) "+
            "values "+
            "(#{template_type}, #{version}, #{create_time}, #{create_user}, #{update_time}, #{update_user}, #{status}, #{sort},#{flow_no})"
    )
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insertTemplateVersion(TbTemplateVersion tbTemplateVersion);


    @Update("<script> " +
            "update " + DB7TableName.TB_TEMPLATE_VERSION +
            " set " +
            "status = #{status} " +
            "where flow_no = #{flow_no} " +
            "</script>")
    Integer updateTemplateVersionStatus(TbTemplateVersion tbTemplateVersion);

    @Update("<script> " +
            "update " + DB7TableName.TB_TEMPLATE_VERSION +
            " set " +
            "status = #{status} " +
            "where template_type = #{template_type} and version = #{version} " +
            "</script>")
    Integer publishTemplateVersionStatus(TbTemplateVersion tbTemplateVersion);

    
    
}
